'use strict';

    angular.module('app').controller('roleUpdateController', ['$scope', '$rootScope', '$state', '$timeout', 'toaster', 
            function($scope, $rootScope, $state, $timeout, toaster) {
            	$scope.record ={};
                $scope.modernBrowsers = [];
                var allselected = "";
                if($state.includes('**.role.update')) {
                	$scope.loading = true;
                    $scope.title = "编辑角色";
                    $scope.reset = false;//修改不允许重置
                    var id = $state.params.id;
                    activate(id);
                    $scope.pageType = 'update';
                 }else if ($state.includes('**.role.create')) {
                    $scope.title = "添加角色";
                    $scope.reset = true;//新增时可以重置
                    $scope.pageType = 'create';
                    initSelect();//请求所有权限
                }
               	validate();//要初始化校验，不然不能保存
               //重置表单
				$scope.resetFrom = function(){
				   $scope.record ={};
				}
             //提交表单
             $scope.submit = function () {
                 if ($scope.pageType == 'create') {
                    $scope._submit_url = '/role/create'
                 } else if ($scope.pageType == 'update') {
                    $scope._submit_url = '/role/update'
                 }

                 $scope.record.permission = allselected;

                 var m = $scope.record;

                 $scope.record.roleId = $state.params.id;
                 if (m) {
                     $scope.isDisabled = true;//提交disabled
                     $.ajax({
                         type: 'POST',
                         dataType: 'json',
                         contentType: 'application/json;charset=UTF-8',
                         url: $scope._submit_url,
                         data: angular.toJson($scope.record)
                     }).then(function(result){
                     	console.log('999')
                     	if (result.httpCode == 200) {//成功
                             //alert("保存成功");
                             toaster.clear('*');
                             toaster.pop('success', '', "保存成功");
                             $timeout(function () {
                                 $state.go('main.sys.role.list');
                             }, 2000);
                         } else {
                             //alert("保存失败");
                             toaster.clear('*');
                             toaster.pop('error', '', result.msg);
                             $scope.isDisabled = false;
                         }
                     });
                 }
             }

                 // 初始化页面
                 function activate(id) {
                     $.ajax({
                         type: 'PUT',
                         dataType: 'json',
                         contentType: 'application/json;charset=UTF-8',
                         url: '/role/read/detail',
                         data: angular.toJson({'id': id})
                     }).then(function (result) {
                         $scope.loading = false;
                         if (result.httpCode == 200) {
                             $scope.record = result.data;
                         } else {
                             $scope.msg = result.msg;
                         }
                         $scope.$apply();
                     });

                     //初始化权限选择下拉框
                     $.get("/role/read/rolepermission?roleId=" + id, function (result, status) {
                         $scope.modernBrowsers = result.data;
                     });


                 }

                 //日期控件
                 $scope.dateOptions = {
                     startingDay: 1,
                     class: 'datepicker'
                 };
                 //日期控件
                 $scope.format = "yyyy-MM-dd";
                 $scope.openDatepicker = function ($event, i) {
                     $event.preventDefault();
                     $event.stopPropagation();
                     if (i == 1) {
                         $scope.beginTime = true;
                     } else if (i == 2) {
                         $scope.endTime = true;
                     }
                 }

	             //表单验证
	             function validate() {
	                 jQuery('form').validate({
	                     rules: {
	                         deptName: {
	                             required: true,
	                             stringCheck: [],
	                             maxLengthB: [20]
	                         },
	                         sortNo: {
	                             required: true
	                         }
	                     },
	                     messages: {
	                         deptName: {
	                             required: '请填写部门名称',
	                             maxLengthB: "部门名称不得超过{0}个字符"
	                         },
	                         sortNo: {
	                             required: '请填写排序'
	                         }
	                     },
	                     submitHandler: function () {
	                        $scope.submit();
	                     }
	                 });
	             }

                 function initSelect() {
                     $.get("/role/read/allpermission", function (result, status) {
                         $scope.modernBrowsers = result.data;
                     });
                 }

                 $scope.fClose = function () {
                     allselected = $scope.outputBrowsers;
                 }

                 //角色唯一性校验
                 $scope.testRoleName = function () {
                     var roleName = $('#roleName').val();
                     console.log(roleName);
                     if ($state.includes('**.role.create')) {//如果是新增就校验，编辑直接跳出
                         $.ajax({
                             type: 'PUT',
                             dataType: 'json',
                             contentType: 'application/json;charset=UTF-8',
                             url: '/role/queryRoleName',
                             data: angular.toJson({'roleName':roleName})
                         }).then(function (result) {
                             if (result.httpCode == 200) {//成功
                                 console.log(result)
                         }else if(result.httpCode == 666){
                                 console.log(result.httpCode,result)
                                 alert('角色名称已经存在，换一个试试！')
                                 $('#roleName').val('');
                             }
                             else {
                                 console.log(result)
                                 toaster.clear('*');
                                 toaster.pop('error', '', result.msg);
                                 $('#roleName').val('');
                             }
                         });
                     }else{

                            return;
                     }
                 }
					


   	}]);